<template>
  <div class="playlist-container">
    <div class="top-wrap">
      <div class="img-wrap">
        <!-- 封面 -->
        <img :src="playlist.coverImgUrl" alt="" />
      </div>
      <div class="info-wrap">
        <p class="title">{{ playlist.name }}</p>
        <div class="author-wrap">
          <!-- 头像 -->
          <img class="avatar" :src="playlist.creator.avatarUrl" alt="" />
          <span class="name">{{ playlist.creator.nickname }}</span>
          <span class="time">{{ playlist.createTime | dateForm }} 创建</span>
        </div>
        <div class="play-wrap">
          <span class="iconfont icon-circle-play"></span>
          <span class="text">播放全部</span>
        </div>
        <div class="tag-wrap">
          <span class="title">标签:</span>
          <!-- 分类标签 -->
          <ul>
            <li v-for="(item, index) in playlist.tags" :key="index">
              {{ item }}
            </li>
          </ul>
        </div>
        <div class="desc-wrap">
          <span class="title">简介:</span>
          <span class="desc">{{ playlist.description }}</span>
        </div>
      </div>
    </div>
    <el-tabs v-model="activeIndex">
      <el-tab-pane label="歌曲列表" name="1">
        <table class="el-table playlit-table">
          <thead>
            <th></th>
            <th></th>
            <th>音乐标题</th>
            <th>歌手</th>
            <th>专辑</th>
            <th>时长</th>
          </thead>
          <tbody>
            <tr
              class="el-table__row"
              v-for="(item, index) in tableData"
              :key="item.id"
            >
              <td>{{ index + 1 }}</td>
              <td>
                <div class="img-wrap" @click="playMusic(item.id)">
                  <img :src="item.al.picUrl" alt="" />
                  <span class="iconfont icon-play"></span>
                </div>
              </td>
              <td>
                <div class="song-wrap">
                  <div class="name-wrap">
                    <span>{{ item.name }}</span>
                    <!-- mv图标 -->
                    <span
                      v-if="item.mv != 0"
                      @click="toMV(item.mv)"
                      class="iconfont icon-mv"
                    ></span>
                  </div>
                  <span>{{ item.subTitle }}</span>
                </div>
              </td>
              <td>{{ item.ar[0].name }}</td>
              <td>
                {{ item.al.name }}
              </td>
              <td>{{ item.dt | timeForm }}</td>
            </tr>
          </tbody>
        </table>
      </el-tab-pane>
      <el-tab-pane :label="`评论(${countCom})`" name="2">
        <!-- 精彩评论 -->
        <div class="comment-wrap">
          <p class="title">
            精彩评论<span class="number">({{ total }})</span>
          </p>
          <div class="comments-wrap">
            <div class="item" v-for="(item, index) in hotComment" :key="index">
              <div class="icon-wrap">
                <img :src="item.user.avatarUrl" alt="" />
              </div>
              <div class="content-wrap">
                <div class="content">
                  <span class="name">{{ item.user.nickname }}：</span>
                  <span class="comment">{{ item.content }}</span>
                </div>
                <div
                  class="re-content"
                  v-for="(i, id) in item.beReplied"
                  :key="id"
                >
                  <span class="name">{{ i.user.nickname }}：</span>
                  <span class="comment">{{ i.content }}</span>
                </div>
                <div class="date">{{ item.time | dateFormat }}</div>
              </div>
            </div>
          </div>
        </div>
        <!-- 最新评论 -->
        <div class="comment-wrap">
          <p class="title">
            最新评论<span class="number">({{ count }})</span>
          </p>
          <div
            class="comments-wrap"
            v-for="(item, index) in NewComment"
            :key="index"
          >
            <div class="item">
              <div class="icon-wrap">
                <img :src="item.user.avatarUrl" alt="" />
              </div>
              <div class="content-wrap">
                <div class="content">
                  <span class="name">{{ item.user.nickname }}：</span>
                  <span class="comment">{{ item.content }}</span>
                </div>
                <div
                  class="re-content"
                  v-for="(i, id) in item.beReplied"
                  :key="id"
                >
                  <span class="name">{{ i.user.nickname }}：</span>
                  <span class="comment">{{ i.content }}</span>
                </div>
                <div class="date">{{ item.tiem | dateFormat }}</div>
              </div>
            </div>
          </div>
        </div>
        <!-- 分页器
      total 总条数
      current-page 当前页
      page-size 每页多少条数据
      current-change 页码改变事件
     -->
        <el-pagination
          @current-change="handleCurrentChange"
          background
          layout="prev, pager, next"
          :total="count"
          :current-page="page"
          :page-size="10"
        ></el-pagination>
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<script>
export default {
  data() {
    return {
      activeIndex: "1",
      // 总条数
      total: 0,
      // 页码
      page: 1,
      //歌单详情
      playlist: {},
      //热门评论
      hotComment: [],
      //热门评论的个数
      total: 0,
      //最新评论
      NewComment: [],
      page: 1,
      //最新评论的总条数
      count: 0,
      tableData: []
    };
  },
  methods: {
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
    },
    //歌单详情
    getSongs() {
      // console.log(this.$route.query.q)
      this.axios
        .get("https://autumnfish.cn/playlist/detail?id=" + this.$route.query.id)
        .then(res => {
          console.log(res);
          this.playlist = res.data.playlist;
          this.tableData = res.data.playlist.tracks;
        })
        .catch(err => console.log(err));
    },
    //获取歌单评论
    getComment() {
      this.axios
        .get("https://autumnfish.cn/comment/hot", {
          params: {
            id: this.$route.query.id,
            type: 2
          }
        })
        .then(res => {
          // console.log(res);
          this.hotComment = res.data.hotComments;
          this.total = res.data.total;
        })
        .catch(err => console.log(err));
    },
    //获取最新评论
    getNewComment() {
      this.axios
        .get("https://autumnfish.cn/comment/playlist", {
          params: {
            id: this.$route.query.id,
            limit: 10,
            offset: (this.page - 1) * 10
          }
        })
        .then(res => {
          console.log(res);
          this.NewComment = res.data.comments;
          this.count = res.data.total;
        })
        .catch(err => console.log(err));
    },
    //改变页面时触发
    handleCurrentChange(newPage) {
      this.page = newPage;
      this.getNewComment();
    },
    playMusic(id) {
      // console.log(id)
      this.axios({
        url: "https://autumnfish.cn/song/url",
        method: "get",
        params: {
          id // id:id
        }
      }).then(res => {
        console.log(res);
        let url = res.data.data[0].url;
        // console.log(this.$parent.musicUrl)
        // 设置给父组件的 播放地址
        this.$parent.url = url;
      });
    },
    toMV(id) {
     this.$router.push('/mv?id=' + id)
    }
  },
  created() {
    //获取歌单详情
    this.getSongs();
    //获取评论信息
    this.getComment();
    //获取最新评论
    this.getNewComment();
  },
  computed: {
    countCom() {
      return this.total + this.count;
    }
  }
};
</script>

<style></style>
